<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('变动时间')" prop="changeDate">
            <DatePicker v-model="form.changeDate" :placeholder="$t('变动时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('事项类型')" prop="itemType">
            <dict-select
              v-model="form.itemType"
              dictionary="holdings_change_item_type"
              :placeholder="$t('事项类型')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司总股数(万股)/注册资本(万)')" prop="totalShare">
            <Money v-model="form.totalShare" unit="w" :placeholder="$t('公司总股数/注册资本')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资主体')" prop="investorId">
            <el-select v-model="form.investorId" filterable :disabled="form.id" :placeholder="$t('投资主体')">
              <el-option
                v-for="(item, index) in investmentOptions"
                :key="index"
                :value="item.id"
                :label="item.investorName"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('持股数量') + $t('(万股)')" prop="numberOfShares">
            <Money v-model="form.numberOfShares" unit="w" :placeholder="$t('持股数量')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('最新持股比例(%)')" prop="latestShareholdingRatio">
            <Ratio v-model="form.latestShareholdingRatio" :placeholder="$t('最新持股比例(%)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('累计分配金额') + $t('金额单位')" prop="accumulatedDistributionAmount">
            <Money v-model="form.accumulatedDistributionAmount" unit="w" :placeholder="$t('累计分配金额')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:holdingschange" :save-show="!form.sourceId" />
    </div>
  </div>
</template>

<script>
    import { saveDealHoldingsChange, getDealHoldingsChange } from '@/api/deal/holdingschange/dealHoldingsChange'
    import { findByDealId } from '@/api/deal/investment/dealInvestment'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
              // 当前项目投资主体
              investmentOptions: [],
                // 表单校验
                rules: {
                  changeDate: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
                  itemType: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
                  // totalShare: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
                  investorId: { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                }
            }
        },
        created() {
            this.getDicts('holdings_change_item_type').then(response => {
                this.itemTypeOptions = response.data
            })
          findByDealId(this.$route.query.id).then(res => {
            this.investmentOptions = res.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                    getDealHoldingsChange(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                }
            },
            // 表单重置
            reset() {
                this.form = {
                  id: undefined,
                  dealId: this.$route.query.id,
                  sourceId: undefined,
                  component: undefined,
                  changeDate: undefined,
                  itemType: undefined,
                  totalShare: undefined,
                  investorId: undefined,
                  numberOfShares: undefined,
                  latestShareholdingRatio: undefined,
                  accumulatedDistributionAmount: undefined,
                  createBy: undefined,
                  createTime: undefined,
                  updateBy: undefined,
                  updateTime: undefined,
                  delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealHoldingsChange(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }

        }

    }
</script>
